/*轮播图*/
$(document).ready(function() {
//------------------trent's slider
    $offset = $('.trent-slider').width();
    $tSlideInStyles = {left: '0', right: '0'}
    $t_loadBarStopStyles = {animation : "none", width : "0%"}
    $hiddenSlideStylesRight = {left: $offset, right: 0 - $offset}
    $hiddenSlideStylesLeft = {right: $offset, left: 0 - $offset}

    //slider functions
    function tStartLoadBar() {$('.t-load-bar .inner-load-bar').css('animation', 'load 4.5s linear infinite');}

    function tSliderHasStopped() {
        if ($('.current-t-slide').css('left') === "0px" && $('.current-t-slide').css('right') === "0px") {
            return true;
        } else {
            return false;
        }
    }

    function tSlideChangerRight() {
        if ($('.current-t-slide').next().hasClass('t-slide') && tSliderHasStopped()) {
            $('.current-t-slide').removeClass('current-t-slide').css($hiddenSlideStylesLeft).next().css($tSlideInStyles).addClass('current-t-slide');
            $('.current-dot').removeClass('current-dot').next().addClass('current-dot');
        } else if (tSliderHasStopped()) {
            $('.current-t-slide').removeClass('current-t-slide');
            $('.t-slide').first().addClass('current-t-slide').css($tSlideInStyles);
            tSetCss();
            $('.current-dot').removeClass('current-dot')
            $('.t-dot').first().addClass('current-dot');
        }
    }
    function tSlideChangerLeft() {
        if ($('.current-t-slide').prev().hasClass('t-slide') && tSliderHasStopped()) {
            $('.current-t-slide').removeClass('current-t-slide').css($hiddenSlideStylesRight).prev().css($tSlideInStyles).addClass('current-t-slide');
            $('.current-dot').removeClass('current-dot').prev().addClass('current-dot');
        } else if (tSliderHasStopped()) {
            $('.current-t-slide').removeClass('current-t-slide');
            $('.t-slide').last().addClass('current-t-slide').css($tSlideInStyles);
            tSetCssLeft();
            $('.current-dot').removeClass('current-dot')
            $('.t-dot').last().addClass('current-dot');
        }
    }

    function tSetCss() {
        $('.t-slide').each(function(index, value) {
            if (index > 0) {
                $(this).css($hiddenSlideStylesRight);
            }
        });
    }
    function tSetCssLeft() {
        $t_total = $('.t-slide').length - 1;
        $('.t-slide').each(function(index, value) {
            if (index < $t_total) {
                $(this).css($hiddenSlideStylesLeft)
            }
        });
    }


    //populate dots for every slide
    $('.t-slide').each(function(index, value) {
        $('.t-slide-dots').append('<div class="t-dot"></div>');
        if (index === 0) {$('.t-dot').first().addClass('current-dot')}
    });

    //slider-code
    $('.trent-slider').css('height', $('.trent-slider').width() / 2);
    tSetCss();
    //load bar
    tStartLoadBar();
    $('.trent-slider').hover(function() {$('.t-load-bar .inner-load-bar').css($t_loadBarStopStyles);}, function() {tStartLoadBar()})
    //interval sllide change
    var tSlideChange = window.setInterval(function() {
        tSlideChangerRight();
    }, 4500);
    $('.trent-slider').mouseover(function() {
        clearInterval(tSlideChange);
    }).mouseout(function() {
        tSlideChange = window.setInterval(function() {
            tSlideChangerRight();
        },4500);
    });

    // -----slider controls
    //arrow
    $('.t-slider-controls .arrow').click(function() {
        if ($(this).hasClass('right-arrow')) {tSlideChangerRight();}
        else if ($(this).hasClass('left-arrow')) {tSlideChangerLeft();}
    });
    //dots
    $('.t-slide-dots .t-dot').click(function() {
        $newDotIndex = $(this).index();
        $currentDotIndex = $('.current-dot').index();
        if (tSliderHasStopped()) {
            $('.t-slide').each(function(index, value) {
                $('.current-dot').removeClass('current-dot');
                $('.current-t-slide').removeClass('current-t-slide');
                $('.t-dot').eq($newDotIndex).addClass('current-dot');
                $('.t-slide').eq($newDotIndex).css($tSlideInStyles).addClass('current-t-slide');
                if (index > $newDotIndex) {
                    $(this).css($hiddenSlideStylesRight);
                } else if (index < $newDotIndex) {
                    $(this).css($hiddenSlideStylesLeft);
                }
            });
        }
    });
    //close slider JS
});


$(document).ready(function () {



    /**
     * 获取并显示news
     * 请求地址：http://39.108.233.68:8002/news/selAll
     * 参数：
     *      新闻标题：detail
     *      新闻发布时间：updateTime
     */
    var news = new Vue({
        el: "#newsContent",
        data: {
            news: []
        },
        created: function () {
            this.getNews();

        },
        methods: {
            getNews: function () {
                var that = this;
                axios.get("http://39.108.233.68:8002/news/selAll").then(
                    function (response) {
                        console.log(response.data.data);
                        that.news = response.data.data;
                        for (var i = 0; i < that.news.length; i++)
                            that.news[i].updateTime = that.news[i].updateTime.substr(0, 9);
                            //that.news[i].updateTime = that.news[i].updateTime.substring(0, 10);
                    },
                    function (err) {
                        console.log(err);
                    }
                )
            },
            onclick: function (newsInfo) {
                console.log(newsInfo);
                window.localStorage.setItem("id", newsInfo.id);
                //$.cookie("id", newsInfo.id);
                //console.log($.cookie("id"));
                window.location.href = "front_news.html";
            }
        }
    });

    /**
     * 最新课程
     * 请求地址：http://39.108.233.68:8002/course/selAllCourse
     */
    var newClass = new Vue({
        el: "#newClass",
        data: {
            newClass: []
        },
        created: function () {
            this.getNewClass();
        },
        methods: {
            getNewClass: function () {
                var that = this;
                axios.get("http://39.108.233.68:8002/course/selAllCourse").then(
                    function (response) {
                        sortKey(response.data.data, "uploadtime");
                        that.newClass = response.data.data;
                        for (var i = 0; i < response.data.data.length; i++) {
                            if (response.data.data[i].image === null)
                                that.newClass[i].image = "img/noPic.jpg";
                            else
                                that.newClass[i].image = "https://www.luckyone.club/software/lesson/" + that.newClass[i].image;
                            //console.log(that.courses[i].image);
                        }
                    }
                )
            },
            /*详情页*/
            onclick: function (courseInfo) {
                console.log(courseInfo);
                //$.cookie("id", courseInfo.id);
                //console.log($.cookie("id"));
                window.localStorage.setItem("id", courseInfo.id);
                window.location.href = "front_course.html";
            }
        }
    });
    
    
    /**
     * 最热课程
     * 请求地址：http://39.108.233.68:8002/course/selAllCourse
     */
    var hot = new Vue({
        el: "#hot",
        data: {
            hot: []
        },
        created: function () {
            this.getHotest();
        },
        methods: {
            getHotest: function () {
                var that = this;
                axios.get("http://39.108.233.68:8002/course/selAllCourse").then(
                    function (response) {
                        console.log(response.data.data);

                        for (var i = 0; i < response.data.data.length; i++)
                            if (response.data.data[i].hot === null)
                                response.data.data[i].hot = 0;
                        sortKey(response.data.data, "hot");
                        for (var i = 0; i < response.data.data.length; i++) {
                            if (response.data.data[i].image === null)
                                response.data.data[i].image = "img/noPic.jpg";
                            else
                                response.data.data[i].image = "https://www.luckyone.club/software/lesson/" + response.data.data[i].image;
                            if (response.data.data[i].hot >= 5)
                                that.hot.push(response.data.data[i]);

                        }

                        console.log("that.hot");

                    }
                )
            },
            onclick: function (courseInfo) {
                console.log(courseInfo);
                //$.cookie("id", courseInfo.id);
                //console.log($.cookie("id"));
                window.localStorage.setItem("id", courseInfo.id);
                window.location.href = "front_course.html";
            }
        }
    });

    function sortKey(array, key) {
        return array.sort(function (a, b) {
            var x = a[key];
            var y = b[key];
            return ((x > y) ? -1 : (x < y) ? 1 : 0)
        })
    }


});